package com.net.xpay.core.validator;

import com.net.common.exception.BusinessException;
import com.net.xpay.core.constant.PoseidonErrorCode;
import com.net.xpay.core.domain.AccountDetail;
import com.net.xpay.core.domain.UserAccount;
import com.net.xpay.core.enums.AccountFundType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @author  on 01/04/2018.
 */
@Component
@Slf4j
public class UserAccountValidator {

    public void validateAccountEnough(UserAccount account, AccountDetail accountDetail) {
        if (accountDetail == null) {
            return;
        }

        if (accountDetail.getChange() < 0L) {
            if (AccountFundType.MONEY.equals(accountDetail.getAccountFundType())) {
                if (account.getMoneyLeft() < 0 || account.getMoneyLeft() + accountDetail.getChange() < 0L) {
                    throw new BusinessException(PoseidonErrorCode.MONEY_NOT_ENOUGH, "服务费账户余额不足，请充值");
                }
            } else if (AccountFundType.TRANS_BATCH.equals(accountDetail.getAccountFundType())) {
                if (account.getTransBatchLeft() < 0 || account.getTransBatchLeft() + accountDetail.getChange() < 0) {
                    throw new BusinessException(PoseidonErrorCode.MONEY_NOT_ENOUGH, "转账次数已用完,请充值");
                }
            }
        }
    }
}
